package hang.xipujewelWeb;

import net.esoar.modules.test.jetty.JettyFactory;
import net.esoar.modules.test.spring.Profiles;

import org.eclipse.jetty.server.Server;

/**
 * 使用Jetty运行调试Web应用, 在Console输入回车快速重新加载应用.
 * 
 * @author calvin
 */
public class ExampleWebServer {

	public static final int PORT = 8089;
	public static final String CONTEXT = "/xipujewelWeb";
	public static final String[] TLD_JAR_NAMES = new String[] { "spring-webmvc" };

	private static void info() {
		System.out.println("[INFO] Server running at http://localhost:" + PORT + CONTEXT);
		System.out.println("[HINT] Hit Enter to reload the application quickly");
		System.out.println("[HINT] Hit k to stop the application quickly");
	}

	public static void main(String[] args) throws Exception {
		// 设定Spring的profile
		Profiles.setProfileAsSystemProperty(Profiles.DEVELOPMENT);
		// 启动Jetty
		Server server = JettyFactory.createServerInSource(PORT, CONTEXT);
		JettyFactory.setTldJarNames(server, TLD_JAR_NAMES);
		try {
			server.start();
			info();
			// 等待用户输入回车重载应用.
			while (true) {
				int c = System.in.read();
				if (107 == c) {// k
					JettyFactory.stopServer(server);
					System.exit(-1);
					break;
				}
				if (c == '\n') {
					JettyFactory.reloadContext(server);
					info();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}

}
